'''
Created on Mar 12, 2011

@author: yonch
'''
import unittest
import spinal
import numpy


class PuncturingScheduleTests(unittest.TestCase):
    def test_001_output_7_4(self):
        
        STRIDED_7_4_OUTPUT = [4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6, 4, 0, 6, 6, 2, 6, 3, 5, 1, 6]
        
        punc = spinal.StridedPuncturingSchedule(7,4)
        output_7_4 = [punc.next() for i in xrange(len(STRIDED_7_4_OUTPUT))]
        
        self.assertEquals(output_7_4[:20], STRIDED_7_4_OUTPUT[:20])
        self.assertTrue(output_7_4 == STRIDED_7_4_OUTPUT, "Output doesn't match reference")

    def test_002_output_32_2(self):
        STRIDED_32_2_OUTPUT = [4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0, 8, 16, 24, 31, 6, 14, 22, 30, 2, 10, 18, 26, 7, 15, 23, 31, 3, 11, 19, 27, 5, 13, 21, 29, 1, 9, 17, 25, 4, 12, 20, 28, 0]

        punc = spinal.StridedPuncturingSchedule(32,2)
        output_32_2 = [punc.next() for i in xrange(len(STRIDED_32_2_OUTPUT))]
        
        self.assertEquals(output_32_2[:20], STRIDED_32_2_OUTPUT[:20])
        self.assertTrue(output_32_2 == STRIDED_32_2_OUTPUT, "Output doesn't match reference")

    def test_003_shorter_than_8_symbols_per_full_pass(self):
        punc = spinal.StridedPuncturingSchedule(2,1)
        output = [punc.next() for i in xrange(200)]
        self.assertTrue(output == [0,1]*100)

        punc = spinal.StridedPuncturingSchedule(3,1)
        output = [punc.next() for i in xrange(300)]
        self.assertTrue(output == [0,2,1]*100)
        
        punc = spinal.StridedPuncturingSchedule(6,2)
        output = [punc.next() for i in xrange(70)]
        self.assertTrue(output == [4, 0, 5, 2, 3, 5, 1] * 10)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.PuncturingScheduleTests']
    unittest.main()